package com.lwz.android.image;

import com.loopj.android.image.SmartImageTask.OnCompleteListener;

import ru.truba.touchgallery.TouchView.InputStreamWrapper.InputStreamProgressListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

@SuppressLint("HandlerLeak")
public class SmartTouchProgressImageView extends RelativeLayout {

	protected ProgressBar mProgressBar;
	protected SmartTouchImageView mImageView;

	protected Context mContext;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			mProgressBar.setProgress(msg.what);
		}
	};

	public SmartTouchProgressImageView(Context ctx) {
		super(ctx);
		mContext = ctx;
		init();

	}

	public SmartTouchProgressImageView(Context ctx, AttributeSet attrs) {
		super(ctx, attrs);
		mContext = ctx;
		init();
	}

	public SmartTouchImageView getImageView() {
		return mImageView;
	}

	protected void init() {
		mImageView = new SmartTouchImageView(mContext);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mImageView.setLayoutParams(params);
		this.addView(mImageView);
		mImageView.setVisibility(GONE);

		mProgressBar = new ProgressBar(mContext, null,
				android.R.attr.progressBarStyleHorizontal);
//		mProgressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_layer));
        params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		params.setMargins(30, 0, 30, 0);
		mProgressBar.setLayoutParams(params);
		mProgressBar.setIndeterminate(false);
		mProgressBar.setMax(100);
		this.addView(mProgressBar);
	}
	
	public void setUrl( Bitmap thumb, String imageUrl, OnCompleteListener l ) {
		if( thumb != null ) {
			mImageView.setVisibility(View.VISIBLE);
			mImageView.setImageBitmap(thumb);
		}
		ProgressWebImage image = new ProgressWebImage(imageUrl);
		mImageView.setImage(image, new MyOnCompleteListener(l));
		image.setOnProgressListener(mInputStreamProgressListener);
	}

	public void setUrl(String imageUrl, OnCompleteListener l) {
		setUrl(null, imageUrl, l);
	}
	
	public void setUrl(String imageUrl) {
		setUrl(null, imageUrl, null);
	}
	
	public void setUrl(Bitmap thumb, String imageUrl) {
		setUrl(thumb, imageUrl, null);
	}
	
	class MyOnCompleteListener extends OnCompleteListener {
		private OnCompleteListener l;
		
		MyOnCompleteListener( OnCompleteListener l ) {
			this.l = l;
		}
		@Override
		public void onComplete(boolean isSuccess, Bitmap bmp) {
			mImageView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.GONE);
			if( l != null ) {
				l.onComplete(isSuccess, bmp);
			}
		}
	};
	
	private InputStreamProgressListener mInputStreamProgressListener = new InputStreamProgressListener() {

		@Override
		public void onProgress(float progressValue, long bytesLoaded,
				long bytesTotal) {
			mHandler.sendEmptyMessage((int) (progressValue * 100));
		}
	};
}
